<?php
/**
 * Created by PhpStorm.
 * User: 共赢岛
 * Date: 2023/12/21
 * Time: 8:51
 */

namespace App\Services\LifeCycle;


use App\Models\Car;
use App\Models\UserCar;
use Illuminate\Support\Facades\Auth;

class CarService
{
    public static function one($id)
    {
        return Car::find($id);
    }

    public static function userCar($user_id = 0)
    {
        $user_car = UserCar::where('user_id', $user_id)->first();
        $user_car->car_title = $user_car->car->title ?? '';
        $user_car->car_number = $user_car->car_number ?? '';

        return $user_car;
    }

    public static function memberAll($condition = [])
    {
        if (isset($condition['order_id']) && $condition['order_id']) {
            $order = OrderService::detail($condition['order_id']);
        } else {
            $order = OrderService::detail();
        }

        if ($order && $order->category->car_ids) {
            $car_ids = json_decode($order->category->car_ids, true);

            $cars = Car::whereIn('id', $car_ids)->get()->toArray();

            foreach ($cars as $key => $value) {
                $new_cars[$key]['value'] = $value['id'];
                $new_cars[$key]['text'] = $value['title'];
            }

            return $new_cars;
        }

        return false;
    }
}